package com.kc.web.controller.business;

import com.alibaba.fastjson.JSONObject;
import com.kc.business.domain.BizImageSize;
import com.kc.business.domain.BizPlatformConsignmentGoods;
import com.kc.business.service.IBizPlatformConsignmentGoodsService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import com.kc.common.utils.ImagesUtils;
import com.kc.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * TODO
 * 馆藏寄卖商品管理
 *
 * @author zhangshuo
 * @version 1.0
 * @date 2020/10/21
 */
@RestController
@Slf4j
@RequestMapping("/business/platformConsignmentGoods")
public class BizPlatformConsignmentGoodsController extends BaseController {
    @Resource
    private IBizPlatformConsignmentGoodsService platformConsignmentGoodsService;

    /**
     * 获取寄卖商品列表
     */
    @PreAuthorize("@ss.hasPermi('business:platformConsignmentGoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizPlatformConsignmentGoods platformConsignmentGoods) {
        platformConsignmentGoods.setIsDeleted(0);
        startPage();
        List<BizPlatformConsignmentGoods> goodsList = platformConsignmentGoodsService.getList(platformConsignmentGoods);
        for (BizPlatformConsignmentGoods goods : goodsList) {
            String[] banners = StringUtils.split(goods.getBannerList(), ",");
            goods.setBannerListArr(banners);
        }
        return getDataTable(goodsList);
    }

    /**
     * 新增寄卖商品
     */
    @PreAuthorize("@ss.hasPermi('business:platformConsignmentGoods:add')")
    @Log(title = "寄卖商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody BizPlatformConsignmentGoods platformConsignmentGoods) {
        Map<String, Integer> map = null;
        try {
            map = ImagesUtils.getImgSize(platformConsignmentGoods.getThumb());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (map == null) {
            return toAjax(0);
        }
        platformConsignmentGoods.setImgWidth(map.get("width"));
        platformConsignmentGoods.setImgHeight(map.get("height"));
        String imageListStr = StringUtils.join(platformConsignmentGoods.getBannerListArr(), ",");
        List<String> imageList = Arrays.asList(platformConsignmentGoods.getBannerListArr());
        List<BizImageSize> imageSizeDTOList = new ArrayList<>();
        imageList.forEach(image -> {
            try {
                Map<String, Integer> mapSize = ImagesUtils.getImgSize(image);
                imageSizeDTOList.add(new BizImageSize(image, mapSize.get("width"), mapSize.get("height")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        platformConsignmentGoods.setBannerListAttr(new JSONObject().toJSONString(imageSizeDTOList));
        platformConsignmentGoods.setBannerList(imageListStr);
        return toAjax(platformConsignmentGoodsService.add(platformConsignmentGoods));
    }

    /**
     * 修改寄卖商品
     */
    @PreAuthorize("@ss.hasPermi('business:platformConsignmentGoods:edit')")
    @Log(title = "寄卖商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@Validated @RequestBody BizPlatformConsignmentGoods platformConsignmentGoods) {
        Map<String, Integer> map = null;
        try {
            map = ImagesUtils.getImgSize(platformConsignmentGoods.getThumb());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (map == null) {
            return toAjax(0);
        }
        platformConsignmentGoods.setImgWidth(map.get("width"));
        platformConsignmentGoods.setImgHeight(map.get("height"));
        String imageListStr = StringUtils.join(platformConsignmentGoods.getBannerListArr(), ",");
        List<String> imageList = Arrays.asList(platformConsignmentGoods.getBannerListArr());
        List<BizImageSize> imageSizeDTOList = new ArrayList<>();
        imageList.forEach(image -> {
            try {
                Map<String, Integer> mapSize = ImagesUtils.getImgSize(image);
                imageSizeDTOList.add(new BizImageSize(image, mapSize.get("width"), mapSize.get("height")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        platformConsignmentGoods.setBannerListAttr(new JSONObject().toJSONString(imageSizeDTOList));
        platformConsignmentGoods.setBannerList(imageListStr);
        return toAjax(platformConsignmentGoodsService.edit(platformConsignmentGoods));
    }

    /**
     * 删除寄卖商品
     */
    @PreAuthorize("@ss.hasPermi('business:platformConsignmentGoods:remove')")
    @Log(title = "寄卖商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{platformConsignmentGoodsId}")
    public AjaxResult remove(@PathVariable Integer platformConsignmentGoodsId) {
        BizPlatformConsignmentGoods bizPlatformConsignmentGoods = new BizPlatformConsignmentGoods();
        bizPlatformConsignmentGoods.setGoodsId(platformConsignmentGoodsId);
        bizPlatformConsignmentGoods.setIsDeleted(1);
        return toAjax(platformConsignmentGoodsService.edit(bizPlatformConsignmentGoods));
    }
}
